//
//  MyArticlesController.m
//  Village
//
//  Created by 程虎 on 2017/9/21.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "MyArticlesController.h"
#import "ArticlesHttpTools.h"
#import "HomeHttpTools.h"
#import "ChildViewCell.h"

@interface MyArticlesController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tabView;
@property (nonatomic, strong) NSMutableArray *dataAry;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageCount;//总页数 至少一页 初始值为2
@end

@implementation MyArticlesController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的文章";
    
    [self config_UI];
}

- (void)config_UI
{
    self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - kNavHeight) style:UITableViewStyleGrouped];
    self.tabView.delegate = self;
    self.tabView.dataSource = self;
    self.tabView.backgroundColor = kBackgroundColor;
    self.tabView.showsVerticalScrollIndicator = NO;
    self.tabView.showsHorizontalScrollIndicator = NO;
    self.tabView.estimatedRowHeight = UITableViewAutomaticDimension;
    self.tabView.estimatedSectionFooterHeight = UITableViewAutomaticDimension;
    self.tabView.estimatedSectionHeaderHeight = 0;
    self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tabView];
    
    if (@available(iOS 11.0, *)) {
        self.tabView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    self.tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshheader_data)];
    
    self.tabView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshfoot_data)];
    [self.tabView.mj_header beginRefreshing];
    [self.tabView registerNib:[UINib nibWithNibName:@"ChildViewCell" bundle:nil] forCellReuseIdentifier:@"ChildViewCell"];
}

#pragma mark - 下拉刷新
- (void)refreshheader_data
{
    self.pageNum = 0;
    self.pageCount = 1;
    self.pageCount = 0;
    [self.tabView.mj_footer setState:MJRefreshStateIdle];
    [self getdata:self.pageNum];
}

#pragma mark - 上拉刷新
- (void)refreshfoot_data
{
    self.pageNum += 1;
    if (self.pageNum < self.pageCount + 1) {
        [self getdata:self.pageNum];
    }
    else {
        [EmptyFunction hideEmpty:self.tabView];
        [self.tabView.mj_footer endRefreshingWithNoMoreData];
    }
}

#pragma mark - TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataAry.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChildViewCell *cell = [ChildViewCell cellWithTableView:tableView];
    cell.model = self.dataAry[indexPath.section];
    cell.nameLab.text = [Globle getInstance].account.nickname.length > 0 ? [Globle getInstance].account.nickname : [Globle getInstance].account.account;
    [cell.icon sd_setImageWithURL:[NSURL URLWithString:[Globle getInstance].headImg.length > 0 ? [Globle getInstance].headImg : [Globle getInstance].visitorIcon] placeholderImage:[UIImage imageNamed:@"user_icon"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self addReadNum:self.dataAry[indexPath.section]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Class currentClass = [ChildViewCell class];
    return [self.tabView cellHeightForIndexPath:indexPath model:self.dataAry[indexPath.section] keyPath:@"model" cellClass:currentClass contentViewWidth:SCREEN_W];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *views = [UIView new];
    views.backgroundColor = [Util colorWithHexString:@"f7f7f7"];
    return views;
}
- (void)getdata:(NSInteger )index
{
    WeakSelf(weakSelf);
    [ArticlesHttpTools GetArticlesWithPageNum:index Success:^(NSDictionary *valueDict) {
        if (index == 0) {
            [weakSelf.tabView.mj_header endRefreshing];
            self.pageCount = [valueDict[@"pagecount"] integerValue];
            self.dataAry = [VillageModel mj_objectArrayWithKeyValuesArray:valueDict[@"list"]];
        }else {
            [weakSelf.tabView.mj_footer endRefreshing];
            NSArray *array = self.dataAry;
            self.dataAry = [NSMutableArray arrayWithArray:array];
            [self.dataAry addObjectsFromArray:[VillageModel mj_objectArrayWithKeyValuesArray:valueDict[@"list"]]];
        }
        [EmptyFunction hideEmpty:self.tabView];
        [weakSelf.tabView reloadData];
    } failure:^(NSString *error) {
        if (index == 0) {
            [weakSelf.tabView.mj_header endRefreshing];
        }
        else {
            [weakSelf.tabView.mj_footer endRefreshing];
        }
        [EmptyFunction showEmpty:self.tabView Height:self.tabView.frame.size.height - self.tabView.tableHeaderView.height];
    }];
}
#pragma mark - 增加阅读数
- (void)addReadNum:(VillageModel *)model
{
    [MBProgressHUD showMessage:@"正在加载..." toView:self.navigationController.view];
    [HomeHttpTools AddHeadlineNewReadNumWith:model Success:^() {
        [MBProgressHUD hideHUDForView:self.navigationController.view];
        [Tools ToNextName:@"MyArtLineViewController" Params:model CurrentVC:self Completion:^(id obj) {
        }];
    } failure:^(NSString *error) {
        [MBProgressHUD hideHUDForView:self.navigationController.view];
    }];
}
@end
